//
//  NSString+category.m
//  sigleAPP
//
//  Created by SKL on 2017/11/17.
//  Copyright © 2017年 SKL. All rights reserved.
//

#import "NSString+category.h"
#import "YJDRSAHandler.h"
@implementation NSString (category)
- (NSString *)yjd_sign
{
    return [[YJDRSAHandler yjd_shareHandler] yjd_signString:self];
}

- (NSString *)yjd_md5
{
    return [[YJDRSAHandler yjd_shareHandler] yjd_md5:self];
}

- (BOOL)yjd_verifySign:(NSString *)sign
{
    return [[YJDRSAHandler yjd_shareHandler] yjd_verifyString:self withSign:sign];
}

- (NSDictionary *)yjd_rc4_dec:(NSString *)string;
{
    return [[YJDRSAHandler yjd_shareHandler] yjd_rc4_dec:self key:[NSString stringWithFormat:@"play101.cn%@",string]];
}

- (NSDictionary *)yjd_dict
{
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    return [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
}

- (NSString *)yjd_urlEncode
{
    if (!self)
    {
        return nil;
    }
    NSString *charactersToEscape = @"?!@#$^&%*+,:;='\"`<>()[]{}/\\|";
    NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
    return [self stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
}

- (NSString *)yjd_path
{
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"YJD" ofType:@"bundle"];
    NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
   return [bundle pathForResource:self ofType:@"png"];
}
- (UIImage *)yjd_image
{
    return [UIImage imageWithContentsOfFile:[self yjd_path]];
}

- (BOOL)yjd_isMobile
{
    NSString *MOBILE = @"^1(3[0-9]|4[579]|5[0-35-9]|7[0135678]|8[0-9])\\d{8}$";
    NSPredicate *mobilePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    return [mobilePredicate evaluateWithObject:self];
}

- (BOOL)yjd_isChinese
{
    NSString *match = @"(^[\u4e00-\u9fa5]+$)";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    return [predicate evaluateWithObject:self];
}

- (BOOL)yjd_isNumberOralphabet
{
    NSString *match = @"[a-zA-Z0-9]*";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    return [predicate evaluateWithObject:self];
}

- (BOOL)yjd_isNumber
{
    NSString *match = @"[0-9]*";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    return [predicate evaluateWithObject:self];
}

- (BOOL)yjd_isIDCart
{
    if (self.length != 18) return NO;
    // 正则表达式判断基本 身份证号是否满足格式
    NSString *regex2 = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";
    NSPredicate *identityStringPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    
    //如果通过该验证，说明身份证格式正确，但准确性还需计算
    if([identityStringPredicate evaluateWithObject:self])
    {
        //** 开始进行校验 *//
        //将前17位加权因子保存在数组里
        NSArray *idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];
        //这是除以11后，可能产生的11位余数、验证码，也保存成数组
        NSArray *idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
        //用来保存前17位各自乖以加权因子后的总和
        NSInteger idCardWiSum = 0;
        for(int i = 0;i < 17;i++) {
            NSInteger subStrIndex  = [[self substringWithRange:NSMakeRange(i, 1)] integerValue];
            NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex:i] integerValue];
            idCardWiSum      += subStrIndex * idCardWiIndex;
        }
        //计算出校验码所在数组的位置
        NSInteger idCardMod=idCardWiSum%11;
        //得到最后一位身份证号码
        NSString *idCardLast= [self substringWithRange:NSMakeRange(17, 1)];
        
        //如果等于2，则说明校验码是10，身份证号码最后一位应该是X
        if(idCardMod==2)
        {
            if(![idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"])
            {
                return NO;
            }
        }
        else
        {
            //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
            if(![idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]])
            {
                return NO;
            }
        }
        return YES;
    }
    return NO;
}

- (NSMutableAttributedString *)yjd_underLineWithRange:(NSRange)range
{
    NSMutableAttributedString *attStr  = [[NSMutableAttributedString alloc] initWithString:self];
    [attStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
    [attStr addAttribute:NSUnderlineColorAttributeName value:[YJDHelper shareHelp].color range:range];
    [attStr addAttribute:NSForegroundColorAttributeName value:[YJDHelper shareHelp].color range:NSMakeRange(range.location - 1, range.length + 2)];
    return attStr;
}

- (CGRect )yjd_getHeightWithWidth:(CGFloat)width withFont:(UIFont *)font
{
    CGSize size = CGSizeMake(width, MAXFLOAT);
    return [self boundingRectWithSize:size
                              options:NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
                           attributes:@{NSFontAttributeName:font}
                              context:nil];
}


@end
